/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Reflection;
using Madason.MetaModel;
using Madason.MetaModel.Solution;
using Madason.MetaModel.Interfaces;
using Madason.MetaModel.Validation;
using System.Text.RegularExpressions;
using Madason.GenUtils;


/// <summary>
/// Summary description for Program
/// </summary>
public class Program
{
	private Madason.MetaModel.Model model;

	//public const string expression = @"Project\(""\{[^}]+\}""\) = ""([^""])"", ""([^""])"", ";
	public const string expression = @"Project\(""\{[^}]+\}""\) = ""([^""]*)"", ""([^""]*)"", ""\{([^}]*)}";
	public static Regex ProjectRegex = new Regex(expression);


	/// <summary>
	/// the current model
	/// </summary>
	public Madason.MetaModel.Model Model
	{
		get { return model; }
		set { model = value; }
	}


	public static Dictionary<string, Guid> FindProjectGuids(string solutionPath)
	{
		using (StreamReader reader = new StreamReader(solutionPath))
		{
			while (reader.EndOfStream == false)
			{
				string line = reader.ReadLine();

			}
		}
		return null;
	}


	/// <summary>
	/// generate maven directories and pom xml files
	/// </summary>
	/// <param name="classes"></param>
	/// <param name="argMap"></param>
	public static void GenerateSolution(SolutionProject project, Dictionary<string, string> argMap, DirectoryInfo dir)
	{
		using (StreamWriter buildWriter = new StreamWriter(dir.FullName + "\\master.build"))
		{
			MasterBuild masterBuild = new MasterBuild();
			masterBuild.Solution = project;
			masterBuild.Render(buildWriter);
			buildWriter.Close();
		}


		using (StreamWriter solWriter = new StreamWriter(dir.FullName + "\\" + project.FullName + ".sln"))
		{
			SolutionFile solutionFile = new SolutionFile();
			solutionFile.Solution = project;
			solutionFile.Render(solWriter);
		}


		foreach (Project childProject in project.ChildProjects)
		{
			childProject.GenerateDirectoryStructure(dir);
		}
	
	}


	/// <summary>
	/// Search recursivly for SolutionProject
	/// </summary>
	/// <param name="ns"></param>
	/// <returns></returns>
	protected static SolutionProject FindSolutionRec(Namespace ns)
	{
		if (ns is SolutionProject) return ns as SolutionProject;

		foreach (Namespace child in ns.Namespaces)
		{
			SolutionProject result = FindSolutionRec(child);
			if (result != null) return result;
		}

		return null;
	}


	/// <summary>
	/// main function
	/// - parse command argumends
	/// - invoke special generator-functions
	/// </summary>
	/// <param name="args"></param>
	[STAThread]
	public static int Main(string[] args)
	{
		GeneratorWorkflow workflow = new GeneratorWorkflow(args);
		workflow.CheckCommandLine();
		workflow.LoadModel();
		workflow.Transform();
		workflow.Validate();

		

		//
		// Generate sources
		//
		SolutionProject project = FindSolutionRec(workflow.Model);
		if (project == null)
		{
			Console.WriteLine("Can't find a SolutionProject in Model");
			return -1;
		}

		GenerateSolution(project, workflow.Arguments, new DirectoryInfo("."));


		return 0;
	}
}
